package com.xintonghua.base.webview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.text.TextUtils;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.xintonghua.base.utils.ProtocolUtils;

/**
 * 页面的配置
 * Created by hao on 16/11/24.
 */
public class MyWebViewClient extends WebViewClient {
    Activity mActivity;
    //页面是否加载完毕
    public boolean pageFinished = false;
    public int ActivityResultRequestCode, ActivityResultResultCode;
    public Intent ActivityResultIntent;
    private final String TEL = "tel:";
    private IWebViewClient ife;

    public MyWebViewClient(Activity activity, IWebViewClient ife) {
        this.ife = ife;
        this.mActivity = activity;
    }

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (ProtocolUtils.isWeb(url)) {
            view.loadUrl(urlAddTime(url));
            ife.shouldOverrideUrlLoading(url);
        } else if (isTelProtocol(url)) {
            callDialTel(url);
        }
        return true;
    }


    /**
     * 追加时间戳
     *
     * @param url 地址
     */
    public String urlAddTime(String url) {
      /*  if (TextUtils.isEmpty(url)) {
            return url;
        }

        if (url.contains("FillCarInfo")) {
            MyUtils.log("进入");
            return url;
        }

        if (url.contains("?")) {//有参数
            url += "&";
        } else {//没有参数
            url += "?";
        }

        MyUtils.log("_________" + url + ("time=" + System.currentTimeMillis()));

        return url + ("time=" + System.currentTimeMillis());*/
        return url;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        pageFinished = false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        pageFinished = true;

        if (ActivityResultRequestCode <= 0) {
            return;
        }

        //不为空，说明有回调
        ife.pageFinished(ActivityResultRequestCode, ActivityResultResultCode, ActivityResultIntent);

        ActivityResultRequestCode = 0;
    }


    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if (errorCode == 404) {

        }
    }


    public void callDialTel(String phoneProtocol) {

        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse(phoneProtocol));
        mActivity.startActivity(intent);
    }


    public boolean isTelProtocol(String url) {
        return protocol(url, TEL);
    }


    public boolean protocol(String url, String protocol) {
        if (!TextUtils.isEmpty(url)) {
            return url.startsWith(protocol);
        }
        return false;
    }


    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();  // 接受所有网站的证书
    }

    public interface IWebViewClient {
        //页面加载完成
        void pageFinished(int RequestCode, int ResultCode, Intent intent);

        //重新加载链接
        void shouldOverrideUrlLoading(String url);
    }
}
